//
//  BCNiceStuffCollectionViewCell.m
//  BangcaiBuy
//
//  Created by Liang Zhang on 16/6/20.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCNiceStuffCollectionViewCell.h"

@implementation BCNiceStuffCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)makeAttributedString {
    NSMutableAttributedString *rmbAttributedString = [[NSMutableAttributedString alloc] initWithString:_rmbLable.text];
    UIColor *textColor = [self randomColor];
    [rmbAttributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
                                value:textColor
                                range:NSMakeRange(0, _price.length)];
    [rmbAttributedString addAttribute:NSForegroundColorAttributeName  //文字颜色
                                value:textColor
                                range:NSMakeRange(_price.length, _unit.length)];
    UIFont *priceFont = [UIFont boldSystemFontOfSize:22.f];
    UIFont *weightFont = [UIFont systemFontOfSize:17.f];
    if ([UIScreen mainScreen].bounds.size.width <= 320.f) {
        priceFont = [UIFont boldSystemFontOfSize:20.f];
        weightFont = [UIFont systemFontOfSize:15.f];
    }
    [rmbAttributedString addAttribute:NSFontAttributeName             //文字字体
                                value:priceFont
                                range:NSMakeRange(0, _price.length)];
    [rmbAttributedString addAttribute:NSFontAttributeName             //文字字体
                                value:weightFont
                                range:NSMakeRange(_price.length, _unit.length)];
    self.rmbLable.attributedText = rmbAttributedString;
    UIFont *nameFont = [UIFont boldSystemFontOfSize:20.f];
    UIFont *discriotionFont = [UIFont systemFontOfSize:13.f];
    if ([UIScreen mainScreen].bounds.size.width <= 320.f) {
        nameFont = [UIFont boldSystemFontOfSize:17.f];
        discriotionFont = [UIFont systemFontOfSize:11.f];
    }
    self.stuffNameLable.font = nameFont;
    self.stuffDiscriptionLable.font = discriotionFont;
}

- (UIColor *)randomColor {
    /*
     128 128 128
     79  106 183
     38  175 167
     193 156 42
     84  167 39
     */
    NSArray *colorPool = @[[UIColor colorWithRed:128.f/255.f green:128.f/255.f blue:128.f/255.f alpha:1],
                           [UIColor colorWithRed: 79.f/255.f green:106.f/255.f blue:183.f/255.f alpha:1],
                           [UIColor colorWithRed: 38.f/255.f green:175.f/255.f blue:167.f/255.f alpha:1],
                           [UIColor colorWithRed:193.f/255.f green:156.f/255.f blue: 42.f/255.f alpha:1],
                           [UIColor colorWithRed: 84.f/255.f green:167.f/255.f blue: 39.f/255.f alpha:1],
                           [UIColor colorWithRed:219.f/255.f green: 89.f/255.f blue: 95.f/255.f alpha:1],
                           [UIColor colorWithRed:174.f/255.f green: 78.f/255.f blue:183.f/255.f alpha:1],
                           [UIColor colorWithRed: 94.f/255.f green: 72.f/255.f blue:177.f/255.f alpha:1],
                           [UIColor colorWithRed: 36.f/255.f green:173.f/255.f blue:149.f/255.f alpha:1],
                           [UIColor colorWithRed:199.f/255.f green:113.f/255.f blue: 37.f/255.f alpha:1]];
    
    int index = [self randomNumber:0 to:9];
    
    return colorPool[index];
}

-(int)randomNumber:(int)from to:(int)to {
    
    return (int)(from + (arc4random() % (to - from + 1)));
}

+ (instancetype)cellWithCollectionView:(UICollectionView *)collectionView {
    static NSString *ID = @"NiceStuffCell";
    UINib *nib = [UINib nibWithNibName:@"BCNiceStuffCollectionViewCell" bundle: [NSBundle mainBundle]];
    [collectionView registerNib:nib forCellWithReuseIdentifier:ID];
    BCNiceStuffCollectionViewCell *cell = [[BCNiceStuffCollectionViewCell alloc] init];
    return cell;
}

@end
